package com.duobaoyu.dby.supply.biz.strategy.message.order.logistics;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.duobaoyu.dby.channel.adapter.enums.BizMessageTypeEnum;
import com.duobaoyu.dby.channel.adapter.enums.ChannelCodeEnum;
import com.duobaoyu.dby.channel.adapter.enums.biz.order.OrderStatusEnum;
import com.duobaoyu.dby.channel.common.util.SupplyCollectionUtils;
import com.duobaoyu.dby.channel.strategy.supply.impl.real.deli.callback.order.DeLiOrderDeliveryCallback;
import com.duobaoyu.dby.supply.biz.constant.OperateSourceConstants;
import com.duobaoyu.dby.supply.biz.constant.OrderOperateConstants;
import com.duobaoyu.dby.supply.biz.producer.SupplyChainMQProducer;
import com.duobaoyu.dby.supply.biz.service.order.OrderService;
import com.duobaoyu.dby.supply.biz.strategy.message.order.base.BaseOrderAsyncHandler;
import com.duobaoyu.dby.supply.common.biz.bo.message.ChannelMessageBO;
import com.duobaoyu.dby.supply.db.entity.order.Order;
import com.duobaoyu.dby.supply.db.repository.order.OrderRepository;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Slf4j
@Component
@RequiredArgsConstructor
public class DeliLogisticsOrderInfoAsyncHandler extends BaseOrderAsyncHandler {

	private final OrderService orderService;
	private final OrderRepository orderRepository;
	private final TransactionTemplate transactionTemplate;
	private final SupplyChainMQProducer supplyChainMQProducer;

	@Override
	protected Set<String> channelCodes() {
		return Sets.newHashSet(ChannelCodeEnum.DE_LI.getValue());
	}

	@Override
	protected Set<Integer> bizMessageTypes() {
		return Sets.newHashSet(BizMessageTypeEnum.LOGISTICS_STATUS_CHANGE.getValue());
	}

	@Override
	protected Boolean executeHandler(ChannelMessageBO channelMessage, Object beforeResult) {
		DeLiOrderDeliveryCallback deLiOrderDeliveryCallback = JSON.parseObject(channelMessage.getOriginalMessage(), DeLiOrderDeliveryCallback.class);

		String orderTradeSn = deLiOrderDeliveryCallback.getOrderId();
		List<Order> orderList = orderRepository.list(Wrappers.<Order>lambdaQuery().eq(Order::getOrderTradeSn, orderTradeSn));
		if (CollectionUtils.isEmpty(orderList)) {
			log.info("message:{},channelCode:{},data:{}","物流回调失败",channelMessage.getChannelCode(),JSON.toJSONString(channelMessage));
			return Boolean.FALSE;
		}

		Order order = SupplyCollectionUtils.getSingleDataFirst(orderList, Order::new);

		OrderStatusEnum orderSubStatusOld = OrderStatusEnum.getOrderSubStatusEnum(order.getOrderStatus());
		OrderStatusEnum orderSubStatusNew = OrderStatusEnum.getOrderSubStatusEnum(OrderStatusEnum.SHIPPED.getValue());
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(@NotNull TransactionStatus transactionStatus) {
				orderService.updateStatusByOrderSnList(Collections.singletonList(order.getOrderSn()), orderSubStatusNew, orderSubStatusOld, OperateSourceConstants.OPERATION_CHANNEL, OrderOperateConstants.CHANNEL_ORDER_SHIPMENT,null);
			}
		});
		supplyChainMQProducer.orderAlterSend(order.getSupplyTradeSn(), order.getServerName());
		//售后处理
		this.afterSaleHandle(order);
		return Boolean.TRUE;
	}
}
